package com.hk.rabbitmq.eight;

import com.hk.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: dpb
 * @date: 2022/3/22
 */
public class EmitLogTopic {
    private static  String EXCHANGE_NAME = "topic_logs";
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

        Map<String, String> bindingKeyMap = new HashMap<>();
        bindingKeyMap.put("quick.orange.rabbit","被Q1Q2接收到");
        bindingKeyMap.put("lazy.orange.elephant","被Q1Q2接收到");
        bindingKeyMap.put("quick.orange.fox","被Q1接收到");
        bindingKeyMap.put("lazy.brown.fox","被Q2接收到");

        bindingKeyMap.put("lazy.pink.rabbit","虽然满足两个绑定但只被队列Q2接收一次");
        bindingKeyMap.put("quick.brown.fox","不匹配任何绑定不会被任何队列接收到会被丢弃");
        bindingKeyMap.put("quick.orange.male.rabbit","是四个单词不匹配绑定会被丢弃");
        bindingKeyMap.put("lazy.orange.male.rabbit","是四个单词但匹配Q2");
        bindingKeyMap.put("lazy.orange","会被谁接收呢？");

        for (Map.Entry<String, String> bindingKeyEntry : bindingKeyMap.entrySet()) {

            String routingKey = bindingKeyEntry.getKey();
            String message = bindingKeyEntry.getValue();
            channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
            System.out.println("生产者发出消息：" + message);
        }
    }
}
